package com.gupao.gupaoeduvippattern.javaBase.thread.threadLocal3;

/**
 * @author Administrator
 * @date 2020-12-08 17:16
 */
public class ThreadLocalExample {

    public static class MyRunnable implements Runnable {

        private static ThreadLocal threadLocal = new ThreadLocal();

        public static ThreadLocal getThreadLocal() {
            return threadLocal;
        }


        /*
           它们访问的是一个ThreadLocal对象，因此这两个线程都无法看到对方保存的值。
           也就是说，它们存取的是两个不同的值
         */
        @Override
        public void run() {
            threadLocal.set((int) (Math.random() * 100D));
            /*try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {

            }*/
            System.out.println(threadLocal.get());

            TLUtil tlUtil = new TLUtil();
            tlUtil.select();
        }
    }

    public static void main(String[] args) {
        MyRunnable sharedRunnableInstance = new MyRunnable();
        Thread thread1 = new Thread(sharedRunnableInstance);
        Thread thread2 = new Thread(sharedRunnableInstance);
        thread1.start();
        thread2.start();

        ThreadLocal tl = new ThreadLocal();
//        System.out.println("数据：" + tl.get());

    }


}
